package view;

import dao.customerDao;
import model.Customer;
import util.dbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @program: foodApp
 * @description: It can customer auto login
 * @author: PanKer
 * @create: 2021-11-22 19:28
 */

@WebServlet("/autoLogin")
public class autoLogin extends HttpServlet {
    private static final long serialVersionUID = -2484065362370819435L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        boolean loginCheck = false;
        String username = null, password = null;
        Cookie cookies[] = req.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) username = cookie.getValue();
                if (cookie.getName().equals("password")) password = cookie.getValue();
            }
        }

        Customer customer;
        if (username != null && password != null) {
            Connection conn = null;
            try {
                conn = dbUtil.getConn();
                if (customerDao.isExist(conn, username)) {
                    customer = new Customer(username, password);
                    customer = customerDao.login(conn, customer);
                    if (customer != null) {
                        loginCheck = true;
                        System.out.println("登录成功");
                    } else {
                        loginCheck = false;
                        System.out.println("密码错误");
                    }
                } else {
                    System.out.println("该用户名不存在");
                }
            } catch (ClassNotFoundException e) {
                System.out.println("ClassNotFoundException错误");
                e.printStackTrace();
            } catch (SQLException e) {
                System.out.println("SQLException错误");
                e.printStackTrace();
            } finally {
                try {
                    System.out.println("数据库已关闭");
                    dbUtil.closeConn(conn);
                } catch (SQLException e) {
                    System.out.println("数据库关闭失败");
                    e.printStackTrace();
                }
            }
        }

        if (loginCheck) {
            writer.println(
                    // 动态页面

                    " <!DOCTYPE html> " + "\n" +
                            "<html lang='en'> " + "\n" +
                            "  " + "\n" +
                            " <head> " + "\n" +
                            " <meta charset='UTF-8'> " + "\n" +
                            " <meta http-equiv='X-UA-Compatible' content='IE=edge'> " + "\n" +
                            "                      <meta name='viewport' content='width=device-width, initial-scale=1.0'> " + "\n" +
                            "                       <link rel='stylesheet' href='https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css'> " + "\n" +
                            "                        <script src='https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js'></script>" + "\n" +
                            "                       <script src='https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js'></script> " + "\n" +
                            "                       <script src='https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js'></script>" + "\n" +
                            "                       <title>adminVerification</title> " + "\n" +
                            "                      <link rel='shortcut icon' href='img/Icon.ico' type='image/x-icon'> " + "\n" +
                            " </head> " + "\n" +
                            " <body>" + "\n" +
                            "<h1>好，" + username + "你已经登录了</h1>" + "\n" +
                            " </body>" + "\n" +
                            "</html>"
            );
        } else resp.sendRedirect(req.getContextPath() + "/html/customer_login.html");
    }
}
